package com.example.listviewshangxia;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.listviewshangxia.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity implements IXListViewListener {
	private XListView mListView;
	private SimpleAdapter mAdapter1;
	private Handler mHandler;
	private ArrayList<HashMap<String, Object>> dlist;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		/** ����ˢ�£��������� */
		dlist = new ArrayList<HashMap<String, Object>>();
		mListView = (XListView) findViewById(R.id.techan_xListView);// ���listview�������layout����
		mListView.setPullLoadEnable(true);// ��������������FALSEΪ�����������㲻���ظ�������
		mAdapter1 = new SimpleAdapter(MainActivity.this, getData(),
				R.layout.scenic_item_list, new String[] { "name", "img",
						"content" }, new int[] { R.id.title, R.id.img,
						R.id.content });
		mListView.setAdapter(mAdapter1);
		mListView.setXListViewListener(this);
		mHandler = new Handler();
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), "�������"+data[position-1], Toast.LENGTH_LONG).show();
			}
		});
	}
	
	/** ��ʼ���������� */
	String data[] = new String[] { "����ʯ����ɭ�ֹ�԰", "��ɽ������ˮ���羰��", "С¹�������¾���",
			"��Ůɽ�羰��", "��������ժ԰" };
	String data1[] = new String[] { "��˳�ؾȱ�����ľ��", "��˳�ؾȱ�����ľ��", "��˳�ؾȱ�����ľ��",
			"��˳�ؾȱ�����ľ��", "��˳�ؾȱ�����ľ��" };

	private ArrayList<HashMap<String, Object>> getData() {

		for (int i = 0; i < data.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", data[i]);
			map.put("content", data1[i]);
			map.put("img", R.drawable.ic_launcher);
			dlist.add(map);
		}
		return dlist;
	}

	/** ֹͣˢ�£� */
	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		mListView.setRefreshTime("�ո�");
	}

	// ˢ��
	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				getData();
				mListView.setAdapter(mAdapter1);
				onLoad();
			}
		}, 2000);
	}

	// ���ظ���
	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				getData();
				mAdapter1.notifyDataSetChanged();
				onLoad();
			}
		}, 2000);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            this.finish();
         }
         return false;
	}
	
}
